/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.IllegalOrphanException;
import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Exiger2;
import java.util.ArrayList;
import java.util.Collection;
import com.applicatioN.entities.MaitriserSoft;
import com.applicatioN.entities.Software;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class SoftwareJpaController implements Serializable {

    public SoftwareJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Software software) throws PreexistingEntityException, Exception {
        if (software.getExiger2Collection() == null) {
            software.setExiger2Collection(new ArrayList<Exiger2>());
        }
        if (software.getMaitriserSoftCollection() == null) {
            software.setMaitriserSoftCollection(new ArrayList<MaitriserSoft>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Exiger2> attachedExiger2Collection = new ArrayList<Exiger2>();
            for (Exiger2 exiger2CollectionExiger2ToAttach : software.getExiger2Collection()) {
                exiger2CollectionExiger2ToAttach = em.getReference(exiger2CollectionExiger2ToAttach.getClass(), exiger2CollectionExiger2ToAttach.getExiger2PK());
                attachedExiger2Collection.add(exiger2CollectionExiger2ToAttach);
            }
            software.setExiger2Collection(attachedExiger2Collection);
            Collection<MaitriserSoft> attachedMaitriserSoftCollection = new ArrayList<MaitriserSoft>();
            for (MaitriserSoft maitriserSoftCollectionMaitriserSoftToAttach : software.getMaitriserSoftCollection()) {
                maitriserSoftCollectionMaitriserSoftToAttach = em.getReference(maitriserSoftCollectionMaitriserSoftToAttach.getClass(), maitriserSoftCollectionMaitriserSoftToAttach.getMaitriserSoftPK());
                attachedMaitriserSoftCollection.add(maitriserSoftCollectionMaitriserSoftToAttach);
            }
            software.setMaitriserSoftCollection(attachedMaitriserSoftCollection);
            em.persist(software);
            for (Exiger2 exiger2CollectionExiger2 : software.getExiger2Collection()) {
                Software oldSoftware1OfExiger2CollectionExiger2 = exiger2CollectionExiger2.getSoftware1();
                exiger2CollectionExiger2.setSoftware1(software);
                exiger2CollectionExiger2 = em.merge(exiger2CollectionExiger2);
                if (oldSoftware1OfExiger2CollectionExiger2 != null) {
                    oldSoftware1OfExiger2CollectionExiger2.getExiger2Collection().remove(exiger2CollectionExiger2);
                    oldSoftware1OfExiger2CollectionExiger2 = em.merge(oldSoftware1OfExiger2CollectionExiger2);
                }
            }
            for (MaitriserSoft maitriserSoftCollectionMaitriserSoft : software.getMaitriserSoftCollection()) {
                Software oldSoftware1OfMaitriserSoftCollectionMaitriserSoft = maitriserSoftCollectionMaitriserSoft.getSoftware1();
                maitriserSoftCollectionMaitriserSoft.setSoftware1(software);
                maitriserSoftCollectionMaitriserSoft = em.merge(maitriserSoftCollectionMaitriserSoft);
                if (oldSoftware1OfMaitriserSoftCollectionMaitriserSoft != null) {
                    oldSoftware1OfMaitriserSoftCollectionMaitriserSoft.getMaitriserSoftCollection().remove(maitriserSoftCollectionMaitriserSoft);
                    oldSoftware1OfMaitriserSoftCollectionMaitriserSoft = em.merge(oldSoftware1OfMaitriserSoftCollectionMaitriserSoft);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findSoftware(software.getSoftware()) != null) {
                throw new PreexistingEntityException("Software " + software + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Software software) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Software persistentSoftware = em.find(Software.class, software.getSoftware());
            Collection<Exiger2> exiger2CollectionOld = persistentSoftware.getExiger2Collection();
            Collection<Exiger2> exiger2CollectionNew = software.getExiger2Collection();
            Collection<MaitriserSoft> maitriserSoftCollectionOld = persistentSoftware.getMaitriserSoftCollection();
            Collection<MaitriserSoft> maitriserSoftCollectionNew = software.getMaitriserSoftCollection();
            List<String> illegalOrphanMessages = null;
            for (Exiger2 exiger2CollectionOldExiger2 : exiger2CollectionOld) {
                if (!exiger2CollectionNew.contains(exiger2CollectionOldExiger2)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger2 " + exiger2CollectionOldExiger2 + " since its software1 field is not nullable.");
                }
            }
            for (MaitriserSoft maitriserSoftCollectionOldMaitriserSoft : maitriserSoftCollectionOld) {
                if (!maitriserSoftCollectionNew.contains(maitriserSoftCollectionOldMaitriserSoft)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain MaitriserSoft " + maitriserSoftCollectionOldMaitriserSoft + " since its software1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Exiger2> attachedExiger2CollectionNew = new ArrayList<Exiger2>();
            for (Exiger2 exiger2CollectionNewExiger2ToAttach : exiger2CollectionNew) {
                exiger2CollectionNewExiger2ToAttach = em.getReference(exiger2CollectionNewExiger2ToAttach.getClass(), exiger2CollectionNewExiger2ToAttach.getExiger2PK());
                attachedExiger2CollectionNew.add(exiger2CollectionNewExiger2ToAttach);
            }
            exiger2CollectionNew = attachedExiger2CollectionNew;
            software.setExiger2Collection(exiger2CollectionNew);
            Collection<MaitriserSoft> attachedMaitriserSoftCollectionNew = new ArrayList<MaitriserSoft>();
            for (MaitriserSoft maitriserSoftCollectionNewMaitriserSoftToAttach : maitriserSoftCollectionNew) {
                maitriserSoftCollectionNewMaitriserSoftToAttach = em.getReference(maitriserSoftCollectionNewMaitriserSoftToAttach.getClass(), maitriserSoftCollectionNewMaitriserSoftToAttach.getMaitriserSoftPK());
                attachedMaitriserSoftCollectionNew.add(maitriserSoftCollectionNewMaitriserSoftToAttach);
            }
            maitriserSoftCollectionNew = attachedMaitriserSoftCollectionNew;
            software.setMaitriserSoftCollection(maitriserSoftCollectionNew);
            software = em.merge(software);
            for (Exiger2 exiger2CollectionNewExiger2 : exiger2CollectionNew) {
                if (!exiger2CollectionOld.contains(exiger2CollectionNewExiger2)) {
                    Software oldSoftware1OfExiger2CollectionNewExiger2 = exiger2CollectionNewExiger2.getSoftware1();
                    exiger2CollectionNewExiger2.setSoftware1(software);
                    exiger2CollectionNewExiger2 = em.merge(exiger2CollectionNewExiger2);
                    if (oldSoftware1OfExiger2CollectionNewExiger2 != null && !oldSoftware1OfExiger2CollectionNewExiger2.equals(software)) {
                        oldSoftware1OfExiger2CollectionNewExiger2.getExiger2Collection().remove(exiger2CollectionNewExiger2);
                        oldSoftware1OfExiger2CollectionNewExiger2 = em.merge(oldSoftware1OfExiger2CollectionNewExiger2);
                    }
                }
            }
            for (MaitriserSoft maitriserSoftCollectionNewMaitriserSoft : maitriserSoftCollectionNew) {
                if (!maitriserSoftCollectionOld.contains(maitriserSoftCollectionNewMaitriserSoft)) {
                    Software oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft = maitriserSoftCollectionNewMaitriserSoft.getSoftware1();
                    maitriserSoftCollectionNewMaitriserSoft.setSoftware1(software);
                    maitriserSoftCollectionNewMaitriserSoft = em.merge(maitriserSoftCollectionNewMaitriserSoft);
                    if (oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft != null && !oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft.equals(software)) {
                        oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft.getMaitriserSoftCollection().remove(maitriserSoftCollectionNewMaitriserSoft);
                        oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft = em.merge(oldSoftware1OfMaitriserSoftCollectionNewMaitriserSoft);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = software.getSoftware();
                if (findSoftware(id) == null) {
                    throw new NonexistentEntityException("The software with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Software software;
            try {
                software = em.getReference(Software.class, id);
                software.getSoftware();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The software with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Exiger2> exiger2CollectionOrphanCheck = software.getExiger2Collection();
            for (Exiger2 exiger2CollectionOrphanCheckExiger2 : exiger2CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Software (" + software + ") cannot be destroyed since the Exiger2 " + exiger2CollectionOrphanCheckExiger2 + " in its exiger2Collection field has a non-nullable software1 field.");
            }
            Collection<MaitriserSoft> maitriserSoftCollectionOrphanCheck = software.getMaitriserSoftCollection();
            for (MaitriserSoft maitriserSoftCollectionOrphanCheckMaitriserSoft : maitriserSoftCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Software (" + software + ") cannot be destroyed since the MaitriserSoft " + maitriserSoftCollectionOrphanCheckMaitriserSoft + " in its maitriserSoftCollection field has a non-nullable software1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(software);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Software> findSoftwareEntities() {
        return findSoftwareEntities(true, -1, -1);
    }

    public List<Software> findSoftwareEntities(int maxResults, int firstResult) {
        return findSoftwareEntities(false, maxResults, firstResult);
    }

    private List<Software> findSoftwareEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Software.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Software findSoftware(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Software.class, id);
        } finally {
            em.close();
        }
    }

    public int getSoftwareCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Software> rt = cq.from(Software.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
